#include "RegionGrid.h"
#include "../../../models/ProjectData.h"
#include "../../../controller/SignalsBus.h"

RegionGrid::RegionGrid(int roiRow, int roiCol, QSize roiSize, int spacing, QObject* parent):QObject(parent),QGraphicsItemGroup(nullptr),row(roiRow),col(roiCol)
{
    setHandlesChildEvents(false);
    int rowCount = ProjectData::getInstance()->fovRowCount;
    int colCount = ProjectData::getInstance()->fovColCount;
    QSize fovSize(roiSize.width()/rowCount,roiSize.height()/colCount);

    fovCount=0;
    for(int row = 0;row<rowCount;row++)
    {
        for(int col=0;col<colCount;col++)
        {
            FovCell* cell = new FovCell(roiRow,roiCol,row,col,fovSize);
            cell->setPos(col*fovSize.width(),row*fovSize.height());
            addToGroup(cell);
            connect(cell,&FovCell::fovClicked,this,&RegionGrid::onRoiClicked);

            fovHash.insert(fovCount,cell);
            fovCount++;

        }
    }
    setPos(roiCol*(roiSize.width()+spacing),roiRow*(roiSize.height()+spacing));

    // qDebug()<<"Fov count "<<fovCount;

}

void RegionGrid::onRoiClicked(int roiRow, int roiCol, int fovRow, int fovCol)
{
    QString msg;
    msg = QString("fov (%1,%2),(%3,%4) be clicked").arg(roiRow).arg(roiCol).arg(fovRow).arg(fovCol);
    qDebug()<<msg;

    emit roiClicked(roiRow,roiCol,fovRow,fovCol);
}
